const pool = require("../connection/sqlPool");


//正在施工中......
// async function MATCHING_1(incomingData) {
//   const promisePool = pool.promise();
//   try{
//   const [rows, fields] = await promisePool.query(`SELECT * FROM Ricipe`);
//   const { food, tool } = incomingData;
//   // console.log(rows);
//   return rows.filter(recipe => {
//     // console.log(recipe);
//     let b=[];
//     let c=[];
//     b.push(recipe.foods);
//     c.push(recipe.tools);
//     console.log("bc",b,c);

//     const incomingFoodArray = [...incomingData.food];
//     const incomingToolArray = [...incomingData.tool];

//     // 检查食谱中的食材是否完全匹配传入的食材，且没有额外的食材
//     const foodMatch = b.every(foodItem => incomingFoodArray.includes(foodItem)) 
//     // &&                     b.length === incomingFoodArray.length;

//     // 检查食谱中的工具是否完全匹配传入的工具，且没有额外的工具
//     const toolMatch = c.every(toolItem => incomingToolArray.includes(toolItem)) 
//     // &&                    c.length === incomingToolArray.length;

//     return foodMatch && toolMatch;
//   });
// }catch(err){
//   return err.message
// }
// }



  async function MATCHING_2(incomingData ) {
    const promisePool = pool.promise();
    try{
    const { food, tool } = incomingData;
    const [rows,fileds] = await promisePool.query(`SELECT * FROM Ricipe`);

    return rows.filter(recipe => {
        // 检查食材是否匹配
        const isFoodMatch = food.every(item => recipe.foods.includes(item));
        // 检查工具是否匹配
        const isToolMatch = tool.every(item => recipe.tools.includes(item));
        // 只有当食材和工具都匹配时，才返回这个data
        return isFoodMatch && isToolMatch;
    });
  }catch(err){
    return err.message
  }
}


module.exports = {MATCHING_2};